package demo;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.sql.*;
/*
 * Created by JFormDesigner on Tue May 19 18:23:06 CST 2020
 */

/**
 * @author Dctor
 */
public class TranslationsFrame extends JFrame {

    public TranslationsFrame() {
        initComponents();
        combo.setEnabled(true);
        combo.insertItemAt("英汉", 0);
        combo.insertItemAt("汉英", 1);
        combo.setSelectedIndex(0);
    }

    private void button1ActionPerformed(ActionEvent e) {
        // TODO add your code here
        String str = Text.getText();
        e.getActionCommand();
        String regex1 = "[\\u4e00-\\u9fa5]+";
        String regex2 = "[A-Za-z]+";
        Connection con;
        ResultSet rs;
        boolean flag = true;
        if (combo.getSelectedItem().equals("英汉")) {
            if (str.matches(regex2)) {
                try {
                    Class.forName("com.mysql.cj.jdbc.Driver");
                    String uri = "jdbc:mysql://localhost:3306/translations?&useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8";
                    String user = "root";
                    String password = "123456";
                    con = DriverManager.getConnection(uri, user, password); //连接代码

                    String sqlStr = "select * from entoch where en like ?";
                    PreparedStatement pre = con.prepareStatement(sqlStr);
                    pre.setString(1, "_%" +str);

                    rs = pre.executeQuery(); //查
                    while (rs.next()) {
                        Translation.setText(rs.getString(2) + "\n" + rs.getString(3));
                        flag = false;
                    }
                    if (flag == true) {
                        Translation.setText("!!!DON'T FOUND!!!");
                    }
                    con.close();

                } catch (Exception e1) {
                    System.out.println(e1);
                }

            } else {
                Translation.setText("");
                JOptionPane.showMessageDialog(this, "!!!FORMAT DON'T MATCH!!!", "MESSAGE",
                        JOptionPane.WARNING_MESSAGE);
                Text.setText(null);
            }

        }
        if (combo.getSelectedItem().equals("汉英")) {
            if (str.matches(regex1)) {
                try {
                    Class.forName("com.mysql.cj.jdbc.Driver");
                    String uri = "jdbc:mysql://localhost:3306/translations?&useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8";
                    String user = "root";
                    String password = "123456";
                    con = DriverManager.getConnection(uri, user, password); //连接代码

                    String sqlStr = "select * from entoch where ch like ?";
                    PreparedStatement pre = con.prepareStatement(sqlStr);
                    pre.setString(1, "_%" + str + "_%");

                    rs = pre.executeQuery(); //查
                    while (rs.next()) {

                        Translation.setText(rs.getString(1) + "\n" + rs.getString(3));
                        flag = false;
                    }
                    if (flag == true) {
                        Translation.setText("!!!DON'T FOUND!!!");
                    }
                    con.close();

                } catch (Exception e1) {
                    System.out.println(e1);
                }


            } else {
                Translation.setText("");
                JOptionPane.showMessageDialog(this, "!!!FORMAT DON'T MATCH!!!", "MESSAGE",
                        JOptionPane.WARNING_MESSAGE);
                Text.setText(null);
            }
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label1 = new JLabel();
        Text = new JTextField();
        Search = new JButton();
        scrollPane1 = new JScrollPane();
        Translation = new JTextArea();
        label2 = new JLabel();
        textField1 = new JTextField();
        scrollPane2 = new JScrollPane();
        combo = new JComboBox();

        //======== this ========
        setBackground(new Color(255, 153, 153));
        setForeground(Color.pink);
        setTitle("Translator");
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u8bf7\u8f93\u5165\uff1a");
        contentPane.add(label1);
        label1.setBounds(10, 45, 110, label1.getPreferredSize().height);
        contentPane.add(Text);
        Text.setBounds(135, 40, 330, 25);

        //---- Search ----
        Search.setText("\u786e\u5b9a");
        Search.addActionListener(e -> button1ActionPerformed(e));
        contentPane.add(Search);
        Search.setBounds(new Rectangle(new Point(490, 40), Search.getPreferredSize()));

        //======== scrollPane1 ========
        {

            //---- Translation ----
            Translation.setEditable(false);
            Translation.setFont(Translation.getFont().deriveFont(Translation.getFont().getSize() + 5f));
            scrollPane1.setViewportView(Translation);
        }
        contentPane.add(scrollPane1);
        scrollPane1.setBounds(135, 85, 330, 290);

        //---- label2 ----
        label2.setText("\u7ffb\u8bd1\uff1a");
        contentPane.add(label2);
        label2.setBounds(10, 90, 75, label2.getPreferredSize().height);

        //---- textField1 ----
        textField1.setText("By Dctor \u7248\u6743\u6240\u6709@@+");
        textField1.setForeground(new Color(204, 0, 0));
        contentPane.add(textField1);
        textField1.setBounds(new Rectangle(new Point(180, 400), textField1.getPreferredSize()));
        contentPane.add(scrollPane2);
        scrollPane2.setBounds(5, 30, 480, 415);
        contentPane.add(combo);
        combo.setBounds(490, 90, 100, combo.getPreferredSize().height);

        { // compute preferred size
            Dimension preferredSize = new Dimension();
            for (int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label1;
    private JTextField Text;
    private JButton Search;
    private JScrollPane scrollPane1;
    private JTextArea Translation;
    private JLabel label2;
    private JTextField textField1;
    private JScrollPane scrollPane2;
    private JComboBox combo;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
